package com.unitd.base.module.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.unitd.base.module.comm.web.validator.Assert;
import com.unitd.base.module.consumer.comm.annotation.IgnoreAuth;
import com.unitd.base.module.consumer.comm.controller.ApiBaseAction;
import com.unitd.base.module.master.service.UserAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * API登录授权前端控制接口
 *
 * @author hudan
 * @version 1.0
 * @filename ApiAuthController.java
 * @copyright www.unitd.com
 * @date 2020-07-01
 */
@Api(tags = "A-登录授权管理")
@RestController
@RequestMapping("/api/auth")
@Slf4j
public class ApiAuthController extends ApiBaseAction {

    @Autowired
    private UserAuthService userAuthService;

    /**
     * 处理用户根据手机号码和密码登录
     *
     * @param mobile   登录手机号码
     * @param password 登录密码
     * @return 登录结果
     */
    @IgnoreAuth
    @PostMapping("login")
    @ApiOperation(value = "登录接口")
    public Map<String, Object> login(String mobile, String password) {
        Assert.isBlank(mobile, "手机号不能为空");
        Assert.isBlank(password, "密码不能为空");

        return userAuthService.login(mobile, password);
    }

    /**
     * 微信登录
     */
    @ApiOperation(value = "微信登录")
    @IgnoreAuth
    @PostMapping("login_by_weixin")
    public Map<String, Object> loginByWeixin() {
        JSONObject jsonParam = this.getJsonRequest();

        return userAuthService.loginBywx(jsonParam, this.getClientIp());
    }
}